var App = getApp()
const hours=[],mins=[];
for (let i = 0; i <= 23; i++) {
	if(i<10){
		hours.push('0'+i)
	}else{
		hours.push(i)
	}
}
for (let i = 0; i <= 59; i++) {
	if(i<10){
		mins.push('0'+i)
	}else{
		mins.push(i)
	}
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    role_id:wx.getStorageSync('role_id'),
    domain:App.globalData.iconsUrl,
		list:[],
		list2:[],
		// 弹窗
		show: false,
		value: [],
		hours,
		mins,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 获取数据列表
    this.getDataList();
  },
  //获取数据列表
  getDataList(){
    let that = this;
    let obj = {b_id:App.get_b_id(),from:1}
    App.request('/Manage/Management/settingStoreList',obj,function(e){
      wx.stopPullDownRefresh()
        if (e.code == "200") {
          that.setData({list:e.result,list2:e.result})
        } 
    })
	},
	// 显示弹窗
	picker_show(e){
		var list2 = this.data.list2
		var store_id = e.currentTarget.dataset.id
		var value = [0,0,0,0,0,0,0]
		let that = this
		list2.forEach(function(element) {
			if(element.store_id==store_id){
					let h = element.auto_cheap_start.split(":")
					value[0]=that.getIndex(h[0],that.data.hours)
					value[2]=that.getIndex(h[1],that.data.mins)
					h = element.auto_cheap_end.split(":")
					value[4]=that.getIndex(h[0],that.data.hours)
					value[6]=that.getIndex(h[1],that.data.mins)
			}
		});
		this.setData({
			show:true,
			value,
			store_id
		})
	},
	// 隐藏弹窗
	exit(){
		this.setData({show:false})
	},
	// 触发选择
	bindChange(e){
		console.log(e)
		let store_id = this.data.store_id
		var list2 = this.data.list2
		let that = this
		let value = e.detail.value
		if(value.length==0){
			return
		}
		list2.forEach(function(element,index) {
			if(element.store_id==store_id){
					list2[index].auto_cheap_start = that.data.hours[value[0]]+':'+that.data.mins[value[2]]
					list2[index].auto_cheap_end   = that.data.hours[value[4]]+':'+that.data.mins[value[6]]
				}
		});
		that.setData({list2})
	},
	switchChange(e){
		console.log(e)
		var list2 = this.data.list2
		let store_id = e.currentTarget.dataset.id
		let that = this
		list2.forEach(function(element,index) {
			if(element.store_id==store_id){
				list2[index].auto_is_cheap=e.detail.value?1:0
			}
		})
		that.setData({list2})
	},
  // 提交保存
  submit(e){
    console.log(e)
			let data =e.detail.value
			let store_id=e.currentTarget.dataset.id
			let that =  this
      let obj = {
        store_id:e.currentTarget.dataset.id,
        member_once:data.member_once,
				self_min:data.self_min,
				timeout_price:data.timeout_price,

				cheap_member_once:data.cheap_member_once,
				cheap_self_min:data.cheap_self_min,
				cheap_timeout_price:data.cheap_timeout_price,

				nomember_once:data.nomember_once,
				nomember_self_min:data.nomember_self_min,
				nomember_timeout_price:data.nomember_timeout_price,
			}
			let list = that.data.list
			list.forEach(function(element) {
				if(element.store_id==store_id){
					obj.auto_is_cheap=element.auto_is_cheap
					obj.auto_cheap_start=element.auto_cheap_start
					obj.auto_cheap_end=element.auto_cheap_end
				}
			})
      App.request('/Manage/Management/setAutoStandard',obj,function(e){
          if (e.code == "200") {
            App.showSuccessToast(e.msg, function() {
              that.getDataList()          //刷新列表
            });
          } 
      })
	},
	// 根据value获取在对象中的index
	getIndex(v,obj){
		for (const key in obj) {
			if (obj.hasOwnProperty(key) && obj[key]==v) { // 确保属性是对象自己的，不是继承的
					return key
			}
		}
		return 0
	},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getDataList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})